const DB = require('../../utils/db');

module.exports = {
	list: async (keywords) => {
		let sql = `select id, name, remark, date_format(create_time,'%Y-%m-%d %H:%i:%s') createTime, 
                  date_format(update_time,'%Y-%m-%d %H:%i:%s') updateTime from t_acticle_type`;
		if (keywords) {
			sql = sql + ` where name like '%${keywords}%'`;
		}
		let [list] = await DB.query(sql);
		return list;
	},
	add: async (name, remark) => {
		let [list] = await DB.query('select name from t_acticle_type where name=?', [name]);
		if (list.length) throw new Error('分类已存在');
		await DB.query('insert into t_acticle_type (name, remark) values(?,?)', [name, remark]);
	},
	edit: async (name, remark, id) => {
		await DB.query(`update t_acticle_type set name=?, remark=? where id =?`, [name, remark, id]);
	},
	del: async (id) => {
		await DB.query('delete from t_acticle_type where id=?', [id]);
	},
	detail: async (id) => {
		let [list] = await DB.query('select id,name, remark from t_acticle_type where id=?', [id]);
		if (!list.length) throw new Error('分类ID有误');
		return list[0];
	}
};
